package creative.fire.interview.amazon.prepare;

/**
 * 1 2 3 
 * 4 5 6 
 * 7 8 9 
 * 
 * print:1 2 3 6 9 8 7 4 5
 * 
 * @author feuyeux@gmail.com 2011-3-21
 */
public class CockwisePrint {

	// private final static int[][] a = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
	private final static int[][] a = new int[][] { 
		{ 11, 12, 13, 14, 15 }, 
		{ 21, 22, 23, 24, 25 }, 
		{ 31, 32, 33, 34, 35 },
		{ 41, 42, 43, 44, 45 }, 
		{ 51, 52, 53, 54, 55 } 
	};

	public static void main(String[] args) {
		int left = 0;
		int right = a.length - 1;
		int top = 0;
		int bottom = a.length - 1;
		basicPrint(top, bottom, left, right);
	}

	private static void basicPrint(int top, int bottom, int left, int right) {
		if (top > bottom || left > right)
			return;
		for (int i = left; i <= right; i++) {
			for (int j = left; j <= right; j++) {
				if (i == top) {
					System.out.print(a[i][j] + " ");
				} else if (i == bottom) {
					System.out.print(a[i][(a[i].length - 1 - j)] + " ");
				} else if (j == right) {
					System.out.print(a[i][j] + " ");
				}
			}
		}

		for (int i = bottom - 1; i > left; i--) {
			System.out.print(a[i][left] + " ");
		}

		basicPrint(++top, --bottom, ++left, --right);
	}

	private static void basicPrint0() {
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.print(a[i][j] + " ");
			}
		}
	}
}
